package com.xd.cloud.service.core;

import com.xd.cloud.dao.core.DataAuthorityDao;
import com.xd.cloud.dao.core.OrgDao;
import com.xd.cloud.dao.core.OrgUserDao;
import com.xd.cloud.dao.core.UserDataAuthorityDao;
import com.xd.cloud.entity.core.DataAuthority;
import com.xd.cloud.entity.core.Org;
import com.xd.cloud.entity.core.OrgUser;
import com.xd.cloud.entity.core.UserDataAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangtao
 * @class_name DataAuthorityService
 * @description 数据权限Service层
 * @create 2017/12/27
 */
@Service
public class DataAuthorityService {
    @Autowired
    private DataAuthorityDao dataAuthorityDao;

    @Autowired
    private UserDataAuthorityDao userDataAuthorityDao;

    @Autowired
    private OrgDao orgDao;
    @Autowired
    private OrgUserDao orgUserDao;

    public DataAuthority get(Integer id){
        return dataAuthorityDao.get(id);
    }

    /**
     * 根据用户id获取访问权限数据
     * @param userId
     * @return
     */
    public String getDataAuthorityByUserId(Integer userId){
        List<UserDataAuthority> userDataAuthorityByUserId = userDataAuthorityDao.getUserDataAuthorityByUserId(userId);
        String roleStr = "";
        for(UserDataAuthority userDataAuthority : userDataAuthorityByUserId){
            roleStr += userDataAuthority.getDataAuthority().getRule()+",";
        }
        if(roleStr != "" && roleStr.length()>1){
            roleStr =roleStr.substring(0,roleStr.length()-1);
        }else{
            OrgUser orgUser = orgUserDao.getByuserid(userId);
            int companyid = orgUser.getCompanyid();
            Org org = orgDao.get(companyid);
            roleStr = orgDao.getSubOrgByOrg(org);
        }
        return roleStr;
    }
}
